﻿using System.Collections.Concurrent;

namespace Awsc.DataAcquisitionSys.Core.Service.DataAcquisition.DataCollectors
{
    public class SensorDynamicSettings
    {
        private SensorDynamicSettings()
        {

        }
        private static SensorDynamicSettings instance = new SensorDynamicSettings();
        public static SensorDynamicSettings Instance
        {
            get
            {
                return instance;
            }
        }

        public ConcurrentDictionary<string, ConcurrentDictionary<string, object>> sensorSettings = new ConcurrentDictionary<string, ConcurrentDictionary<string, object>>();
        public Dictionary<string, object> GetSetting(string sensorName)
        {
            if (string.IsNullOrEmpty(sensorName) || !sensorSettings.ContainsKey(sensorName))
            {
                return new Dictionary<string, object>();
            }
            return sensorSettings[sensorName].ToDictionary();
        }
        public object GetSettingValue(string sensorName, string settingName)
        {
            if (string.IsNullOrEmpty(sensorName) || !sensorSettings.ContainsKey(sensorName) || string.IsNullOrWhiteSpace(settingName))
            {
                return default;
            }
            var settings = sensorSettings[sensorName];
            if (settings == null || !settings.ContainsKey(settingName))
            {
                return default;
            }


            return sensorSettings[sensorName][settingName];
        }

        public void AddOrUpdateSettingValue(string sensorName,string settingName,object value)
        {
            if(string.IsNullOrWhiteSpace(sensorName)||string.IsNullOrWhiteSpace(settingName)||value==null)
            {
                return;
            }

            if(!sensorSettings.ContainsKey(sensorName))
            {
                sensorSettings[settingName] = new ConcurrentDictionary<string, object>();
            }
            sensorSettings[settingName][settingName] = value;
        }

    }
}
